/**
 * Copyright (c) 2021-2028, iron.guo 郭成杰 (jiedreams@sina.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.beluga.notice.sms;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.beluga.notice.constant.AliyunConstant;
import com.beluga.notice.props.SmsConfiguration;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;


@Slf4j
public class SMSClient {

    @Autowired
    private SmsConfiguration smsConfiguration;


    /**
     * 发送阿里云短信
     * @param params
     * @return
     * @throws ClientException
     */
    @SneakyThrows
    public boolean sendSMS(SMSEntity params){
        Assert.notNull(smsConfiguration.getEndpoint(), "'endpoint' must be not null");
        Assert.notNull(smsConfiguration.getAccessKey(), "'accessKeyId' must be not null");
        Assert.notNull(smsConfiguration.getSecretKey(), "'accessKeySecret' must be not null");
        /**
         * 连接阿里云
         */
        DefaultProfile profile = DefaultProfile.getProfile(this.smsConfiguration.getEndpoint(), this.smsConfiguration.getAccessKey(), this.smsConfiguration.getSecretKey());
        IAcsClient client = new DefaultAcsClient(profile);
        /**
         * 构建请求
         */
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(AliyunConstant.smsDomain);
        request.setSysVersion(AliyunConstant.smsVersion);
        request.setSysAction(AliyunConstant.smsAction);
        /**
         * 自定义参数 (手机号，验证码，签名，模板）
         */
        request.putQueryParameter("PhoneNumbers", params.getPhone());
        request.putQueryParameter("SignName", params.getSignName());
        request.putQueryParameter("TemplateCode", params.getTemplateCode());
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(params.getSmsParams()));
        CommonResponse response = client.getCommonResponse(request);
        return response.getHttpResponse().isSuccess();
    }
}
